<?php

namespace CircularHelper;

use \App\Model\Circular;
use Illuminate\Database\QueryException;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

/**
 * Date: 2018/7/11
 * @author Leon
 * @param string $type
 * @param array $user_id
 * @param array|null $content
 * @return \Illuminate\Http\JsonResponse
 */
function SendCircular(string $type, array $user_id, ?array $content)
{
    $notice = checked($type, $content);
    if(isset($notice->original['resultCode'])){
        if ($notice->original['resultCode'] == 400) {
            return failed($notice->original['resultMessage']);
        }
    }

    switch ($type) {
        case "attendance":
            $information['title'] = "新的一天，离你开始实现今天梦想时间还有10分钟，加油哦！";
            break;

        case "scheduling":
            $information['title'] = "任务执行安排已更新， 请注意查看！";
            break;

        case "attendance_examined_agree":
            $information['title'] = '您的“' . $content['type'] . '“申诉被批准，签到时间更变为：' . $content['time'] . '，签到状态更新为：' . $content['status'] . '。';
            $information['status'] = "已同意";
            break;

        case "attendance_examined_refuse":
            $information['title'] = '您的“' . $content['type'] . '“申诉被拒绝！';
            $information['status'] = "不通过";
            $information['reason'] = $content['reason'];
            break;

        case "suspend_agree":
            $information['title'] = '您的中止申请已通过！';
            $information['content']['task'] = $content['task'];
            $information['content']['type'] = $content['type'];
            $information['content']['time'] = $content['time'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['status'] = "已同意";
            break;

        case "suspend_refuse":
            $information['title'] = '您的中止申请不通过！';
            $information['content']['task'] = $content['task'];
            $information['content']['type'] = $content['type'];
            $information['content']['time'] = $content['time'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['status'] = "不通过";
            $information['reason'] = $content['reason'];
            break;

        case "stop_agree":
            $information['title'] = '您的终止结算申请已通过！';
            $information['content']['task'] = $content['task'];
            $information['content']['status'] = $content['status'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['status'] = "已同意";
            break;

        case "stop_refuse":
            $information['title'] = '您的终止结算申请未通过！';
            $information['content']['task'] = $content['task'];
            $information['content']['status'] = $content['status'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['status'] = "不通过";
            $information['reason'] = $content['reason'];
            break;

        case "receipt_agree":
            $information['title'] = '您的接单申请已通过！';
            $information['content']['task'] = $content['task'];
            $information['content']['status'] = $content['status'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['status'] = "已通过";
            break;

        case "receipt_refuse":
            $information['title'] = '您的接单申请不通过！';
            $information['content']['task'] = $content['task'];
            $information['content']['status'] = $content['status'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['status'] = "不通过";
            break;

        case "temporary":
            $information['title'] = '您有新的支援任务，情况如下：';
            $information['content']['post'] = $content['post'];
            $information['content']['department'] = $content['department'];
            $information['content']['personnel'] = $content['personnel'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['content']['time'] = $content['time'];
            break;

        case "reward":
            $information['title'] = '感谢你的付出，您的任务奖励已经发放，请注意查收！';
            break;

        case "transfer_agree":
            $information['title'] = '您的请求支援申请已通过，调动情况如下：';
            $information['content']['post'] = $content['post'];
            $information['content']['department'] = $content['department'];
            $information['content']['personnel'] = $content['personnel'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['content']['time'] = $content['time'];
            break;

        case "transfer_refuse":
            $information['title'] = '您的请求支援申请被拒绝！';
            $information['content']['post'] = $content['post'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['content']['time'] = $content['time'];
            $information['status'] = "不通过";
            $information['reason'] = $content['reason'];
            break;

        case "transfer_support":
            $information['title'] = '您的店铺得到支援，情况如下：';
            $information['content']['post'] = $content['post'];
            $information['content']['department'] = $content['department'];
            $information['content']['personnel'] = $content['personnel'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['content']['time'] = $content['time'];
            break;

        case "transfer_send":
            $information['title'] = '您需要派出支援，情况如下：';
            $information['content']['post'] = $content['post'];
            $information['content']['department'] = $content['department'];
            $information['content']['personnel'] = $content['personnel'];
            $information['content']['start_time'] = $content['start_time'];
            $information['content']['end_time'] = $content['start_time'];
            $information['content']['time'] = $content['time'];
            break;

        case "bonus":
            $information['title'] = $content['month'] . '奖金已经下发，请于' . $content['day'] . '前完成分配！';
            break;

        case "business_leader":
            $information['title'] = '您所在的业务组负责人发生了变更，负责人变更为' . $content['post'] . '部门的' . $content['personnel'];
            break;

        case "business_approver":
            $information['title'] = '您所在的业务组发生了变更，审批人变更为' . $content['post'] . '部门的' . $content['personnel'];
            break;

        case "settlement":
            $information['title'] = '还有3天即将达到结算日，请尽快完成本月结算';
            break;

        case "settlement_before":
            $information['title'] = '还有3天即将达到结算日，请尽快完成本月结算';
            break;

        case "settlement_after":
            $information['title'] = $content['store'].'门店奖金分配已完成，请尽快完成分配';
            break;

        default:
            return failed('请传入正确类型');
            break;

    }
    DB::beginTransaction();
    try {
        $data['title'] = $information['title'];
        if (isset($information['content'])) {
            $data['content'] = json_encode($information['content']);
        }
        $data['type'] = $type;
        $data['client'] = config('system.client_id.'.$type);
        $circular = Circular::query()->create($data);
        $circular_id = Circular::query()->find($circular['id']);
        $circular_id->users()->attach($user_id);
        DB::commit();
    } catch (QueryException $ex) {
        DB::rollback();
        return failed('添加通知失败');
    }
}


function checked(string $type, array $content)
{
    $param = config('system.circular.' . $type);
    $param = array_keys($param['content']);
    foreach ($param as $value) {
        if (!isset($content[$value])) {
            return failed('通知中缺少' . $value . '参数');
        }
    }
}





